function $(id) {
    return document.getElementById(id);
}

window.onload = function () {

    findByItem(1);
}

/**
 * 模糊动态分页查询
 * @param pageNo 页码
 */
function findByItem(pageNo) {
  axios.get("/room/findByItem",{
      params:{
          pageNo:pageNo,
          address:$("address").value,
          startPrice:$("startPrice").value,
          endPrice:$("endPrice").value
      }
  }).then(resp =>{
      var str = "";
      var list = resp.data.list;
      list.forEach(e =>{
          str += `<tr><td>${e.address}</td><td>${e.price}</td><td>${e.createDate}</td>
                <td><input type="button" value="删除" onclick="del(${e.id})">
                <input type="button" value="修改" onclick="findById(${e.id})"></td></tr>`;
          $("roomData").innerHTML = str;

          var pageStr = "";
          for (var i = 1 ; i <= resp.data.pages;i++){
                pageStr += `<a href="javascript:findByItem(${i})" style="margin-left: 14px;">${i}</a>`;
                $("pageNo").innerHTML = pageStr;
          }

      })
  })
}

function showAddDiv() {
    $("addDiv").style.visibility = 'visible';
    $("addDiv").style.width = document.documentElement.clientWidth+"px";
    $("addDiv").style.height = document.documentElement.clientHeight+"px";

}
/**
 * 添加房间
 */
function addRoom(){
    axios.get("/room/add",{
        params: {
            address:$("addAddress").value,
            price:$("addPrice").value,
            createDate:$("addCreateDate").value
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            findByItem(1);
            $('addDiv').style.visibility = 'hidden';
            $("addAddress").value = '';
            $("addPrice").value = '';
            $("addCreateDate").value = '';
        }
    });
}

/**
 * 根据id删除
 * @param id 房间id
 */
function del(id) {
    axios.get("/room/del",{
        params:{
            id:id
        }
    }).then(resp =>{
        if ("ok" == resp.data){
            findByItem(1);
        }
    })
}

/**
 * 根据id查询旧数据
 * @param id 房间id
 */
function findById(id) {

    axios.get("/room/findById",{
        params:{
            id:id
        }
    }).then(resp =>{
        var roomObj = resp.data;
        $("id").value = roomObj.id;
        $("spanArddess").innerHTML = roomObj.address;
        $("updatePrice").value = roomObj.price;
        $("spanDate").innerHTML = roomObj.createDate;
    });

    $("updateDiv").style.visibility = 'visible';
    $("updateDiv").style.width = document.documentElement.clientWidth+"px";
    $("updateDiv").style.height = document.documentElement.clientHeight+"px";

}

function updatePrice() {

    axios.get("/room/update",{
        params:{
            id:$("id").value,
            price:$("updatePrice").value,
        }
    }).then(resp =>{
        if ( resp.data == "ok"){
            findByItem(1);
            $("updateDiv").style.visibility = 'hidden';
        }
    })

}
